<?php

jimport('joomla.application.component.controller');

class bgController extends JController {
    
    public function __construct(){
        parent::__construct();
        
        
        $this->model = $this->getModel();
    }
    
    public function display($cachable = false, $urlparams = false) {
        parent::display($cachable, $urlparams);
    }

    public function remove(){
    	$cid = JRequest::getVar('cid');
        
        /* remove file in server */
        foreach($cid as $value){
            //get file name
            $file = $this->getModel()->getFileNameFromINFO_CODE($value);
            //Del file
            if($file != null){
                //ATTACHMENT 1
                if($file->INFO_FILE_1 != null){
                    $encodeFILE1 = sha1($file->INFO_FILE_1);
                    $path = "uploads/files/com_information/$encodeFILE1";
                    if(file_exists($path)){
                        $del = unlink($path);
                    }
                }
                //ATTACHMENT 2
                if($file->INFO_FILE_2 != null){
                    $encodeFILE2 = sha1($file->INFO_FILE_2);
                    $path = "uploads/files/com_information/$encodeFILE2";
                    if(file_exists($path)){
                        $del = unlink($path);
                    }
                }
                //ATTACHMENT 3
                if($file->INFO_FILE_3 != null){
                    $encodeFILE3 = sha1($file->INFO_FILE_3);
                    $path = "uploads/files/com_information/$encodeFILE3";
                    if(file_exists($path)){
                        $del = unlink($path);
                    }
                }
                //ATTACHMENT 4
                if($file->INFO_FILE_4 != null){
                    $encodeFILE4 = sha1($file->INFO_FILE_4);
                    $path = "uploads/files/com_information/$encodeFILE4";
                    if(file_exists($path)){
                        $del = unlink($path);
                    }
                }
                //ATTACHMENT 5
                if($file->INFO_FILE_5 != null){
                    $encodeFILE5 = sha1($file->INFO_FILE_5);
                    $path = "uploads/files/com_information/$encodeFILE5";
                    if(file_exists($path)){
                        $del = unlink($path);
                    }
                }
            }
        }
            
            
        if($this->model->remove($cid)){
            $this->setRedirect('index.php?option=com_information');
        }else{
            //JError::raiseError(500, 'Error ! Can\'t delete' );
            $error = "エラー！データが削除されません。";
            JRequest::setVar('task','');
            JFactory::getApplication()->enqueueMessage($error, 'error');
        }
    }
    
   
   public function save()
   {   
            $data = &JRequest::getVar('jform');
             if(empty($data['MAIL_USER'])){
                //$error = "VUI LONG NHAP MAIL USER";
                $error = " 配信先を入力してください。";                
                JRequest::setVar('task','error');
                JFactory::getApplication()->enqueueMessage($error, 'error');
                parent::display();
                return;
            }
            elseif(empty($_POST['info_subject'])){               
                $error = " 件名を入力してください。";                
                JRequest::setVar('task','error');
                JFactory::getApplication()->enqueueMessage($error, 'error');
                parent::display();
                 return;
                 }            
            elseif(empty($_POST['info_body'])){               
                $error = " インフォメーション本文を入力してください。";                
                JRequest::setVar('task','error');
                JFactory::getApplication()->enqueueMessage($error, 'error');
                parent::display();
                return;
                }
                
    
            else {
                
                $list_=$data['MAIL_USER'];
                $info_title=$_POST['info_subject'];
                $info_body=$_POST['info_body'];
                $info_cain_code=$_POST['listcain'];
                $infodate=date('Y-m-d H:i:s');
                $info_flag=1;
                 $file1=$_POST['info_attach1'];
                 $file2=$_POST['info_attach2'];
                 $file3=$_POST['info_attach3'];
                 $file4=$_POST['info_attach4'];
                 $file5=$_POST['info_attach5'];

                $cid =$_POST['input_id'];
                
                //
                if (empty($cid))
                {
                    $addnew=1;
                    $this->model->save($info_title,$info_body,$info_cain_code,$infodate,$info_flag,$list_,$file1,$file2,$file3,$file4,$file5,$addnew,$cid);
                }
                else
                {
                    $addnew=0;
                    $this->model->save($info_title,$info_body,$info_cain_code,$infodate,$info_flag,$list_,$file1,$file2,$file3,$file4,$file5,$addnew,$cid);
                }
                
                  
                $this->setRedirect('index.php?option=com_information');
         
                parent::display();
            }
    }
    
    public function savetemp(){ 
        
             $data = &JRequest::getVar('jform');
             
                
                $list_=$data['MAIL_USER'];
                $info_title=$_POST['info_subject'];
                $info_body=$_POST['info_body'];
                $info_cain_code=$_POST['listcain'];
                $infodate=date('Y-m-d H:i:s');
                $info_flag=0;
                $file1=$_POST['info_attach1'];
                 $file2=$_POST['info_attach2'];
                 $file3=$_POST['info_attach3'];
                 $file4=$_POST['info_attach4'];
                 $file5=$_POST['info_attach5'];
                /*if(empty($_FILES['info_attach1']['name']))
                {
                    $file1=$_POST['old_info_attach1'];
                }
                else
                {
                    $file1 = $_FILES['info_attach1']['name'];
                    $path = "uploads/files/com_information/".sha1($_POST['old_info_attach1']);
                        if(file_exists($path)){ 
                            $del = unlink($path);
                        }
                }
                
                if(empty($_FILES['info_attach2']['name']))
                {
                    $file2=$_POST['old_info_attach2'];
                }
                else
                {
                    $file2 = $_FILES['info_attach2']['name'];
                    $path = "uploads/files/com_information/".sha1($_POST['old_info_attach2']);
                        if(file_exists($path)){ 
                            $del = unlink($path);
                        }
                }
                if  (empty($_FILES['info_attach3']['name']))
                {
                    $file3=$_POST['old_info_attach3'];
                }
                else
                {
                    $file3=$_FILES['info_attach3']['name'];
                    $path = "uploads/files/com_information/".sha1($_POST['old_info_attach3']);
                        if(file_exists($path)){ 
                            $del = unlink($path);
                        }
                }
                
                
                if  (empty($_FILES['info_attach4']['name']))
                {
                    $file4=$_POST['old_info_attach4'];
                }
                else
                {
                    $file4=$_FILES['info_attach4']['name'];
                    $path = "uploads/files/com_information/".sha1($_POST['old_info_attach4']);
                        if(file_exists($path)){ 
                            $del = unlink($path);
                        }
                }
                
                if  (empty($_FILES['info_attach5']['name']))
                {
                    $file5=$_POST['old_info_attach5'];
                }
                else
                {
                    $file5=$_FILES['info_attach5']['name'];
                    $path = "uploads/files/com_information/".sha1($_POST['old_info_attach5']);
                        if(file_exists($path)){ 
                            $del = unlink($path);
                        }
                }*/
                $cid =$_POST['input_id'];
                
                //
                if (empty($cid))
                {
                    $addnew=1;
                    $this->model->save($info_title,$info_body,$info_cain_code,$infodate,$info_flag,$list_,$file1,$file2,$file3,$file4,$file5,$addnew,$cid);
                }
                else
                {
                    $addnew=0;
                    $this->model->save($info_title,$info_body,$info_cain_code,$infodate,$info_flag,$list_,$file1,$file2,$file3,$file4,$file5,$addnew,$cid);
                }
                
                    /*// upload file 
                    move_uploaded_file($_FILES["info_attach1"]["tmp_name"],"uploads/files/com_information/" . sha1($_FILES["info_attach1"]["name"]));
                    move_uploaded_file($_FILES["info_attach2"]["tmp_name"],"uploads/files/com_information/" . sha1($_FILES["info_attach2"]["name"]));
                    move_uploaded_file($_FILES["info_attach3"]["tmp_name"],"uploads/files/com_information/" . sha1($_FILES["info_attach3"]["name"]));
                    move_uploaded_file($_FILES["info_attach4"]["tmp_name"],"uploads/files/com_information/" . sha1($_FILES["info_attach4"]["name"]));
                    move_uploaded_file($_FILES["info_attach5"]["tmp_name"],"uploads/files/com_information/" . sha1($_FILES["info_attach5"]["name"]));*/
                    
                $this->setRedirect('index.php?option=com_information');
         
                parent::display();
     
    }
    
    ////search user
//    public function searchUser(){
//        $search['USERNAME'] =  JRequest::getVar('username');
//        $this->search_name = $search['USERNAME'];
//        $result = $this->getModel()->getAllListUser($search);
//        
//        echo '<div class="the_scroll">
//                <table class="adminlist">
//        		<thead>
//        			<tr>
//                        <th class="check"><input id="toggle" name="toggle" type="checkbox" onclick="checkAll(this);checkID();" /></th>
//        				<th width="10%" class="left">
//        					コード 			
//                        </th>
//                        <th width="30%" class="left">
//                            名称 
//                        </th>
//        				<th width="60%" class="left">
//                            名称カナ				
//                        </th>
//        				
//        			</tr>
//        		</thead>
//                <tbody>
//                ';
//                        if($result != null){
//                            foreach($result as $k=>$row){
//                 echo '            
//         		         <tr class="row'.($k%2).'">
//                			<td class="center">
//                                <input class="checkbox-id" type="checkbox" id="cb'.$row->id.'" name="cid[]" value="'.$row->id .'" onclick="isChecked(this.checked);checkID();" />                           
//                            </td>
//                            <td style="display: none;" id="id'.$row->id.'">'.$row->id.'</td>
//                			<td class="left" id="user'.$row->id.'">'.$row->name.'</td>
//                            <td class="left">'.$row->username.'</td>
//                            <td class="left" id="email'.$row->id.'">'.$row->email .'</td>
//                        
//            		    </tr>';
//                        
//                            }
//                        }else{
//                echo '      <tr>
//                                <td class="center" colspan="4">データは見つかりませんでした</td>
//                            </tr>';
//                      
//                        }
//                     
//            	echo '</tbody>
//                   
//                	</table>
//                    <input type="hidden" name="task" value="" />
//            <input type="hidden" name="boxchecked" id="boxchecked" value="0"/>
//            <textarea name="list_uid" id="list_uid" style="display: none;"></textarea>
//                </div>';
//        
//        $app = &JFactory::getApplication();
//        $app->close();
//    }
    
    //Category
    //show add cain display
    public function addCain(){
        
        $id_Max = $this->getModel()->getMaxCain_code();
                
        echo '<div class="m" style="overflow: hidden;margin-bottom:10px;">
                <div  webmemes_parsed="1">
                 <span class="float-left button" style="line-height: 55px; font-weight:bold;float:left">インフォメーションカテゴリ管理画面</span>
        			<div webmemes_parsed="1">
        				<div class="toolbar-list" id="toolbar" webmemes_parsed="1">
                                            
                                <ul>
                                    <li class="button" id="toolbar-save">
                                        <a href="#" id="link_save" class="toolbar">
                                        <span class="icon-32-save" webmemes_parsed="1">
                                        </span>
                                        登録
                                        </a>
                                    </li>
                                    
                                    <li class="button" id="toolbar-cancel">
                                        <a href="#" id="link_cancel" class="toolbar">
                                        <span class="icon-32-cancel" webmemes_parsed="1">
                                        </span>
                                        キャンセル
                                        </a>
                                    </li>                                
                                </ul>
                                
                        </div>
                                					
        			</div>
        	</div>
            </div>
            <div class="clr"> </div>';
        echo '<table>
                <tr>
                    <td width="250px"><label>インフォメーションカテゴリＩＤ</label></td>
                    <td><input type="text" name="CAIN_CODE" id="CAIN_CODE" readonly="true" value="'.++$id_Max.'" class="inputbox medium" style="width:230px" /></td>
                </tr>
                <tr>
                    <td><label>インフォメーションカテゴリ名</label></td>
                    <td><input type="text" name="CAIN_NAME" id="CAIN_NAME" class="inputbox medium" style="width:230px" /></td>
                </tr>
             </table>';
        
        echo '<script>
                jq(document).ready(function() {        
                //save
                jq("#link_save").click(function(){
                        jq.ajax({';
        echo  "            url: 'index.php?option=com_information&task=saveCain'
                            +'&CAIN_CODE='+jq('#CAIN_CODE').val()+'&CAIN_NAME='+encodeURI(jq('#CAIN_NAME').val())+'&addNew=1',
                          context: document.body
                        }).done(function(data ) {
                          jq('#cain_content').html(data);
                        }); 
                })";
        echo '  //save
                jq("#link_cancel").click(function(){
                        jq.ajax({';
        echo  "            url: 'index.php?option=com_information&task=searchCain',
                          context: document.body
                        }).done(function(data ) {
                          jq('#cain_content').html(data);
                        }); 
                })
            });    
            </script>";
        
        $app = &JFactory::getApplication();
        $app->close();
    }
    //show edit cain display
    public function editCain(){
        
        $id = JRequest::getVar('CAIN_CODE');
        $result = $this->getModel()->getCainFromID($id);
        
        echo '<div class="m" style="overflow: hidden;margin-bottom:10px;">
                <div  webmemes_parsed="1">
                 <span class="float-left button" style="line-height: 55px; font-weight:bold;float:left">インフォメーションカテゴリ管理画面</span>
        			<div webmemes_parsed="1">
        				<div class="toolbar-list" id="toolbar" webmemes_parsed="1">
                                            
                                <ul>
                                    <li class="button" id="toolbar-save">
                                        <a href="#" id="link_save" class="toolbar">
                                        <span class="icon-32-save" webmemes_parsed="1">
                                        </span>
                                        登録
                                        </a>
                                    </li>
                                    
                                    <li class="button" id="toolbar-cancel">
                                        <a href="#" id="link_cancel" class="toolbar">
                                        <span class="icon-32-cancel" webmemes_parsed="1">
                                        </span>
                                        キャンセル
                                        </a>
                                    </li>                                
                                </ul>
                                
                        </div>
                                					
        			</div>
        	</div>
            </div>
            <div class="clr"> </div>';
        echo '<table>
                <tr>
                    <td width="250px"><label>インフォメーションカテゴリＩＤ</label></td>
                    <td><input type="text" name="CAIN_CODE" id="CAIN_CODE" readonly="true" value="'.$result->CAIN_CODE.'" style="width:230px" class="inputbox medium" /></td>
                </tr>
                <tr>
                    <td><label>インフォメーションカテゴリ名</label></td>
                    <td><input type="text" name="CAIN_NAME" id="CAIN_NAME" value="'.$result->CAIN_NAME.'" class="inputbox medium" style="width:230px" /></td>
                </tr>
             </table>';
        
        echo '<script>
                jq(document).ready(function() {        
                //save
                jq("#link_save").click(function(){
                        jq.ajax({';
        echo  "            url: 'index.php?option=com_information&task=saveCain'
                            +'&CAIN_CODE='+jq('#CAIN_CODE').val()+'&CAIN_NAME='+encodeURI(jq('#CAIN_NAME').val())+'&addNew=0',
                          context: document.body
                        }).done(function(data ) {
                          jq('#cain_content').html(data);
                        }); 
                })";
        echo '  //save
                jq("#link_cancel").click(function(){
                        jq.ajax({';
        echo  "            url: 'index.php?option=com_information&task=searchCain',
                          context: document.body
                        }).done(function(data ) {
                          jq('#cain_content').html(data);
                        }); 
                })
            });    
            </script>";
        
        $app = &JFactory::getApplication();
        $app->close();
    }
    //save cain
    public function saveCain(){
        $data['CAIN_CODE'] =  JRequest::getVar('CAIN_CODE');
        $data['CAIN_NAME'] = JRequest::getVar('CAIN_NAME');
        $addNew = JRequest::getVar('addNew');
        if($addNew==1){
            $addNew = true;
        }else{
            $addNew = false;
        }
        if($this->model->savecain($data,$addNew)){
            $this->searchCain();
        }else{
            echo "Can't save!";
        }
        
        $app = &JFactory::getApplication();
        $app->close();
    }
    //delete cain
    public function deleteCain(){
        
        $cid = JRequest::getVar('cid');
        $cid = substr($cid,1);  
        if($this->model->removeCain($cid)){
            $this->searchCain();        
        }else{
            echo "Error!!!";
        }
        
        $app = &JFactory::getApplication();
        $app->close();
    }
    
    //show list cain
    public function searchCain(){
        
        $list_cain = $this->model->getAllCainPopup();
        
        echo '<div class="m" style="overflow: hidden;"> 
                <div  webmemes_parsed="1">
                 <span class="float-left button" style="line-height: 55px; font-weight:bold;float:left">インフォメーションカテゴリ管理画面</span>
        			<div webmemes_parsed="1">
        				<div class="toolbar-list" id="toolbar" webmemes_parsed="1">
                                            
                            <ul>
                                <li class="button" id="toolbar-new">
                                    <a href="#" id="link_add" class="toolbar">
                                    <span class="icon-32-new" webmemes_parsed="1">
                                    </span>
                                    作成
                                    </a>
                                </li>
                                
                                <li class="button" id="toolbar-edit">
                                    <a href="#" id="link_edit" class="toolbar">
                                    <span class="icon-32-edit" webmemes_parsed="1">
                                    </span>
                                    編集
                                    </a>
                                </li>
                                
                                <li class="button" id="toolbar-delete">
                                    <a href="#" id="link_delete" class="toolbar">
                                    <span class="icon-32-delete" webmemes_parsed="1">
                                    </span>
                                    削除
                                    </a>
                                </li>
                            
                            </ul>
                                
                        </div>
                                					
        			</div>
        	</div>
            </div>
            <div class="clr"> </div>
            
            <div class="m" style="overflow: hidden; margin-top: 10px;">
            <div id="the_wrap_1">
                <div class="the_scroll">
                   <table class="adminlist">
                		<thead>
                			<tr>
                				<th class="th10">
                					<input type="checkbox" name="checkall-toggle" value="" title="Check All" onclick="checkAll(this)"/>
                                </th>
                                <th class="th30">
                                    ID
                                </th>
                                <th class="th60">
                                    インフォメーションカテゴリ名
                                </th>
                				
                			</tr>
                		</thead>
                                		
                		<tbody>';
                           
                            if($list_cain != null){
                                foreach($list_cain as $k=>$row){
                                         
                    	echo  '<tr class="row'.($k%2).'">
                    			<td class="center">
                                    <input type="checkbox" class="checkbox-id"  id="cb'.$row->CAIN_CODE.'" name="cid[]" value="'. $row->CAIN_CODE.'" onclick="isChecked('.$row->CAIN_CODE.');" />
                                </td>
                    			<td class="text-algin-left"><a href="#" class="item_edit" onclick="jq('."'#cain_code_temp'".').val('.$row->CAIN_CODE.')" >'. $row->CAIN_CODE.'</a></td>
                                <td class="text-algin-left"><a href="#" class="item_edit" onclick="jq('."'#cain_code_temp'".').val('.$row->CAIN_CODE.')" >'. $row->CAIN_NAME.'</a></td>
                    			
                                
                    		  </tr>';
                            
                                }
                            }else{
                            
                        echo    '<tr>
                                    <td class="center" colspan="3">データは見つかりませんでした。</td>
                                </tr>';
                                
                            }
                         
           echo     	'</tbody>
                       
                	</table>
                    <input type="hidden" id="cain_code_temp" value="" />
                    <input type="hidden" name="boxchecked" id="boxchecked" value="0"/>
                </div>
            </div>
            </div>';
        echo '<script>
                function checkAll(a,b){b||(b="cb");if(a.form){for(var c=0,d=0,f=a.form.elements.length;d<f;d++){var e=a.form.elements[d];if(e.type==a.type&&(b&&0==e.id.indexOf(b)||!b))e.checked=a.checked,c+=!0==e.checked?1:0}if(a.form.boxchecked)a.form.boxchecked.value=c;return!0}for(var e=document.adminForm,c=e.toggle.checked,f=a,g=0,d=0;d<f;d++){var h=e[b+""+d];if(h)h.checked=c,g++}document.adminForm.boxchecked.value=c?g:0}';
        echo "  function isChecked(a){ if(document.id('cb'+a).checked==true){document.id('boxchecked').value++;}else{document.id('boxchecked').value--;}};";
                
        echo '    jq(document).ready(function() {
                            
                    //add
                    jq("#link_add").click(function(){
                        
                            jq.ajax({';
        echo "                 url: 'index.php?option=com_information&task=addCain',
                              context: document.body
                            }).done(function(data ) {
                              jq('#cain_content').html(data);
                            }); 
                    });";
                    
        echo '       //edit
                    jq("#link_edit").click(function(){';
        echo "          if(document.id('boxchecked').value==0){
                            alert('編集対象のものを指定してから行ってください。');
                            return false;
                        }  
                        jq( '.checkbox-id' ).each(function() {
                            if(jq(this).attr('checked')){
                                id = jq(this).val();             
                            }
                        });
                        //ajax
                        jq.ajax({
                          url: 'index.php?option=com_information&task=editCain'
                          +'&CAIN_CODE='+id,
                          context: document.body
                        }).done(function(data ) {
                          jq('#cain_content').html(data);
                        }); 
                    });";
        echo '      //load edit display when click cain_code
                    jq(".item_edit").live("click",function(){
                        var id = jq("#cain_code_temp").val();
                        //ajax
                        jq.ajax({';
        echo "            url: 'index.php?option=com_information&task=editCain'
                          +'&CAIN_CODE='+id,
                          context: document.body
                        }).done(function(data ) {
                          jq('#cain_content').html(data);
                        });
                    });";
        echo '      //delete
                    jq("#link_delete").click(function(){';
        echo "              if (document.id('boxchecked').value==0){
                            alert('編集対象のものを指定してから行ってください。');
                            return false;
                        }
                        if(!confirm('選択たのを削除します。よろしいでしょうか？')){
                            return false;
                        }
                        var list_id = '';
                        jq( '.checkbox-id' ).each(function() {
                            if(jq(this).attr('checked')){
                                list_id += ','+jq(this).val();             
                            }
                        });
                        if(list_id != ''){
                            //ajax
                            jq.ajax({
                              url: 'index.php?option=com_information&task=deleteCain'
                              +'&cid='+list_id,
                              context: document.body
                            }).done(function(data ) {
                              jq('#cain_content').html(data);
                            });
                        }
                    });
                    
                    
                });
                  
            
            </script>";
        
        $app = &JFactory::getApplication();
        $app->close();
    }
    
      public function search_user(){
        $filter = array(
            'ocba_name' => JRequest::getVar('ocba_name'),
            'pref_search' => JRequest::getVar('pref_search'),
            'area_search' => JRequest::getVar('area_search'),
            'usertype_search' => JRequest::getVar('usertype_search'),
            );
        $result = $this->getModel()->getAllUserMail($filter);
        
        echo '<div class="the_scroll">
                      <table>
                         <thead>
                            <tr>
                                <th class="check"><input id="toggle" name="toggle" type="checkbox" onclick="checkAll(this);checkID();" /></th>
                                <th class="code">配信先コード</th>
                                <th class="mail">配信先名</th>
                            </tr>
                         </thead>
                         
                         <tbody>';
        if(!empty($result)){
            foreach ($result as $key => $row){
                echo   '<tr>
                           <td class="check">
                                <input class="checkbox-id" type="checkbox" id="cb'.$row->id.'" name="cid[]" value="'.$row->id.'" onclick="isChecked(this.checked);checkID('.$row->id.');" />                           
                           </td>
                            <td class=" code " id="user'.$row->id.'">'.$row->id.'</td>
                           <td class="left" id="email'.$row->id.'">'.$row->name.'</td>
                        </tr>';
            }
        } else {
            echo   '    <tr>
                            <td class="left" colspan="3">データは見つかりませんでした。</td>
                        </tr>';
        }
        
        echo '          </tbody>         
                    </table>
                      </div>
                      <input type="hidden" id="all_user" name="all_user" value="'.$s.'" />
                      <input type="hidden" name="task" value="" />
                      <input type="hidden" name="boxchecked" id="boxchecked" value="0"/>
                      
                      <textarea name="list_uid" id="list_uid"></textarea>
                      ';
        
        $app = &JFactory::getApplication();
        $app->close();
    }
    public function loadCain(){
        $cain_current = JRequest::getVar('cain_current');
        $model = $this->getModel();
        $list_cain = $model->getAllCain();
        
        echo '<select name="listcain" id="listcain" class="medium">';
                foreach($list_cain as $value){
                   echo   '<option value="'. $value->CAIN_CODE. '"'; if($value == $cain_current){ echo 'selected="selected"'; } echo '>'; echo $value->CAIN_NAME; echo '</option>';
                }     
        echo  '</select>';
        
        
        $app = &JFactory::getApplication();
        $app->close();
        exit();
    }
    /* pop up */
    public function selectArea(){
        $area_code = JRequest::getCmd('area_code',0);
        
        $model = $this->getModel();
        $pref = $model->getAllPrefOfArea($area_code);
        echo '<select name="pref" id="pref" class="medium">';
        echo '<option value="0" selected="selected">全て...</option>"';                             
        foreach($pref as $value){                       
            echo '<option value="'.$value->PREF_CODE.'">'.$value->PREF_NAME.'</option>';        
        }                       
        echo '</select>';
        $app = &JFactory::getApplication();
        $app->close();
        exit();
    }    


    
    
    
}